<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Doc转换Pdf</title>
    <style>
        .submitButton {
            margin-top: 20px;
            margin-left: 150px;
            background-color: #e37e10;
            border-radius: 10px;
            border: 1px solid #ff8300;
        }
    </style>
</head>
<body>
<div style="text-align: center">
    <h1>Doc转换Pdf工具</h1>
    <form id="docToPdf" enctype="multipart/form-data" method="post" onsubmit="return allowFileType()">
        <input type="file" id="file" name="file" placeholder="请选择文件" onchange="allowFileType()"
               style="border: 1px solid black;"  accept="application/msword">
        <br>
        <input type="submit" value="转换为pdf文件" class="submitButton" onclick="updateAction();">
    </form>
</div>
</body>
<script>

    //获取项目根路径
    function getRootPath() {
        //获取主机地址之后的目录，如： uimcardprj/share/meun.jsp
        let pathName = window.document.location.pathname;
        //获取带"/"的项目名，如：/uimcardprj
        return pathName.substring(0, pathName.substr(1).indexOf('/') + 1);
    }

    function updateAction() {
        document.getElementById('docToPdf').action = getRootPath() + '/doc/to/pdf';
    }

    function allowFileType() {
        let file = document.getElementById("file").files[0];
        let fileName = file.name;
        console.log(fileName)
        let fileSize = file.size;
        console.log(fileSize)
        let suffix = fileName.substring(fileName.lastIndexOf("."), fileName.length).toLowerCase();
        if ('.doc' != suffix) {
            alert("目前只允许传入.doc格式的文件！");
            return false;
        }
        // if (fileSize > 2 * 1024 * 1024) {
        //     alert("上传图片不允许超过2MB!");
        //     return false;
        // }
        return true;
    }

</script>
</html>
